<?php

class Payserv_RussianBank_Model_RussianBank extends Mage_Directory_Model_Currency_Import_Abstract
{
    
    protected $_url = 'http://www.cbr.ru/scripts/XML_daily.asp';
    protected $_messages = array();
 
    protected function _convert($currencyFrom, $currencyTo, $retry=0)
    {
        try {
			Mage::log('触发汇率RussianBank接口',1, 'rumall.log');
            sleep(1); 
			$ch = curl_init();
			// set URL and other appropriate options
			curl_setopt($ch, CURLOPT_URL, $this->_url);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			// grab URL and pass it to the browser
			$res = curl_exec($ch); /* return xml */
			curl_close($ch);

			if($res){
				$xml=simplexml_load_string($res);
				$data = $xml->Valute;
				foreach($data as $v){
					if($v->CharCode == 'USD'){
						$exchange_rate = (float)str_replace(',', '.', $v->Value);
					}
				}
			}

			if( !$exchange_rate ) {
                $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s', $this->_url);
                return null;
            }
			Mage::log('$exchange_rate',1, 'rumall.log');
			Mage::log($exchange_rate,1, 'rumall.log');
            return (float) $exchange_rate * 1.03; // change 1.0 to influence rate;
        }
        catch (Exception $e) {
            if( $retry == 0 ) {
                $this->_convert($currencyFrom, $currencyTo, 1);
            } else {
                $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s', $url);
            }
        }
    }
}